package com.test.dababy.dababy_00_http;

import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import okhttp3.logging.HttpLoggingInterceptor;


public class Http_dababyLogger implements HttpLoggingInterceptor.Logger {


    public double hptg=0.12290502849884977;
    public float kEph=0.37887448f;
    public String l2PN="";
    public String u4C4="";
    public double v9vv=0.187449924330084;
    public float wQst=0.74740934f;

    public void q1XcZ(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getL2PN()))
                    setU4C4(String.valueOf(getHptg())+getkEph()+getV9vv()+getwQst());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setHptg(double hptg) {
        this.hptg = hptg;
    }

    public void setkEph(float kEph) {
        this.kEph = kEph;
    }

    public void setL2PN(String l2PN) {
        this.l2PN = l2PN;
    }

    public void setU4C4(String u4C4) {
        this.u4C4 = u4C4;
    }

    public void setV9vv(double v9vv) {
        this.v9vv = v9vv;
    }

    public void setwQst(float wQst) {
        this.wQst = wQst;
    }

    public double getHptg() {
        return hptg;
    }

    public float getkEph() {
        return kEph;
    }

    public String getL2PN() {
        return l2PN;
    }

    public String getU4C4() {
        return u4C4;
    }

    public double getV9vv() {
        return v9vv;
    }

    public float getwQst() {
        return wQst;
    }

    @Override
    public void log(@NonNull String s) {
        if (s.length() > 4000) {
            int i = 0;
            while (i < s.length()) {
                if (i + 4000 < s.length()) {
                    Log.i(Http_dababyLogger.class.getSimpleName() + i,
                            formatJson_dababy(s.substring(i, i + 4000)));
                } else {
                    Log.i(Http_dababyLogger.class.getSimpleName() + i,
                            formatJson_dababy(s.substring(i, s.length())));
                }
                i += 4000;
            }
        } else {
            q1XcZ();
            Log.i(Http_dababyLogger.class.getSimpleName(), formatJson_dababy(s));
        }
    }

    public static String formatJson_dababy(String jsonStr) {
        int level = 0;
        StringBuilder jsonForMatStr = new StringBuilder();
        for (int i = 0; i < jsonStr.length(); i++) {
            char c = jsonStr.charAt(i);
            if (level > 0 && '\n' == jsonForMatStr.charAt(jsonForMatStr.length() - 1)) {
                jsonForMatStr.append(getLevelStr_dababy(level));
            }
            switch (c) {
                case '{':
                case '[':
                    jsonForMatStr.append(c).append("\n");
                    level++;
                    break;
                case ',':
                    jsonForMatStr.append(c).append("\n");
                    break;
                case '}':
                case ']':
                    jsonForMatStr.append("\n");
                    level--;
                    jsonForMatStr.append(getLevelStr_dababy(level));
                    jsonForMatStr.append(c);
                    break;
                default:
                    jsonForMatStr.append(c);
                    break;
            }
        }
        return jsonForMatStr.toString();
    }

    public static String getLevelStr_dababy(int level) {
        StringBuilder levelStr = new StringBuilder();
        for (int levelI = 0; levelI < level; levelI++) {
            levelStr.append("\t");
        }
        return levelStr.toString();
    }

}
